/******************************************************************************
Author: Akshay Dixit
Date Created: October 22, 2018
Data last modified: June 17, 2020
File Name: 6_tz.do
Project: T4D Tanzania 
Purpose: Analysis of Village-level intermediate outcomes (Facility data)
******************************************************************************/

/* LIST OF INTERMEDIATE OUTCOMES & THE ASSOCIATED VARIABLES
		
	Note:	The sample has 153 facilities. But there are 152 observations in the facility dataset. 
			This is because 1 treatment facility could not be surveyed. This facility had been closed.
			In addition, only partial data is available from one other dispensary. 

Category 2. Improved facility access (transportation, new facility, longer facility hours, outreach services)
	
	Outcome 1:	Build or request a new facility - If (and how many) new facilities have been built/commissioned in the last 3 years. 
				vf06
	Outcome 2:	Mobile clinic - If (and how many) new mobile maternity clinics have been set-up in the last 3 years.
				vf04 vf05

Category 4. Bylaws, partnerships, or other interventions aimed at health system uptake

	Outcome 1:	If any of the sample villages in the facility's catchment area have a by-law or other local regulation, requiring/encouraging the uptake of MNH services.
				pi14 pi16
				
Category 6. Improved attitude, effort, trust of the provider

	Outcome 1:	CHW/VHW residence in village
				vf01 vf02 vf03				
			
				
				
*/

clear all
set more off
	
u "$data/T4D_end_fc_clean.dta", clear

cd "$analysis"

gl strata = "strata1 strata2 strata3 strata4 strata5 strata6 strata7"

*******************************************************************************

* KEEP VILLAGE LEVEL OUTCOMES, RESHAPE DATA-SET

desc vf04* vf05* vf06* pi14* pi16* vf01* vf02* vf03*
	
	//Prepare treatment, strata variables for reshaping
ren treatment treatment1
g treatment2 = treatment1 
	
ren strata_var strata_var1
g strata_var2 = strata_var1	
	
	//Prepare outcomes for reshaping
ren vf04 mobile_clinic1	
ren vf05 mobile_clinic_recent1
ren vf06 dispensary1
ren pi14 bylaw1
ren vf01 chw_assigned1
ren vf02 chw_number1
ren vf03 chw_residence1
	
ren vf04_2 mobile_clinic2	
ren vf05_2 mobile_clinic_recent2
ren vf06_2 dispensary2
ren pi16 bylaw2
ren vf01_2 chw_assigned2
ren vf02_2 chw_number2
ren vf03_2 chw_residence2

foreach var in mobile_clinic1 mobile_clinic_recent1 dispensary1 bylaw1 chw_assigned1 ///
chw_number1 chw_residence1 mobile_clinic2 mobile_clinic_recent2 dispensary2 bylaw2 ///
chw_assigned2 chw_number2 chw_residence2 {
	tab `var'
}

	//Reshape data-set from wide (facility-level) to long(village-level)
keep health_facility_id treatment* strata_var* village1 village2 ///
mobile_clinic1 mobile_clinic_recent1 dispensary1 bylaw1 chw_assigned1 ///
chw_number1 chw_residence1 mobile_clinic2 mobile_clinic_recent2 dispensary2 bylaw2 ///
chw_assigned2 chw_number2 chw_residence2
reshape long mobile_clinic mobile_clinic_recent dispensary bylaw chw_assigned chw_number chw_residence treatment strata_var village, i(health_facility_id) j(j)
drop j

duplicates tag, gen(_dup)
tab _dup //No total duplicates

drop if mobile_clinic == .	//Drop all superflous rows

duplicates tag village, gen(village_dup)
isid health_facility_id village

count

	//Recreate strata dummies
forvalues i=1/8 {
	g strata`i' = (strata_var == `i')
}
	
*******************************************************************************

* CATEGORY 2. Improved facility access

***Preparing Outcomes 1 and 2***

foreach var in mobile_clinic mobile_clinic_recent dispensary {
		replace `var' = 0 if `var' == 2
		tab `var'
}

replace mobile_clinic_recent = 0 if (mobile_clinic_recent == . & mobile_clinic == 0)

	//Replace missing values of the variable "dispensary" (see label below) with values reported by field team
replace dispensary = 0 if (health_facility_id == 1601 & village == 1602001)
replace dispensary = 0 if (health_facility_id == 1602 & village == 1601002)
replace dispensary = 0 if (health_facility_id == 1604 & village == 1603001)
replace dispensary = 0 if (health_facility_id == 1817 & village == 1828002)
replace dispensary = 0 if (health_facility_id == 2006 & village == 2007002)
replace dispensary = 0 if (health_facility_id == 2019 & village == 2021002)
replace dispensary = 0 if (health_facility_id == 2021 & village == 2023002)
replace dispensary = 1 if (health_facility_id == 1605 & village == 1605002)
replace dispensary = 1 if (health_facility_id == 1607 & village == 1608001)
replace dispensary = 1 if (health_facility_id == 1609 & village == 1602002)
replace dispensary = 1 if (health_facility_id == 1611 & village == 1611002)
replace dispensary = 1 if (health_facility_id == 1815 & village == 1823004)
replace dispensary = 1 if (health_facility_id == 2009 & village == 2011002)
replace dispensary = 1 if (health_facility_id == 2010 & village == 2008001)
replace dispensary = 1 if (health_facility_id == 2011 & village == 2009002)
replace dispensary = 1 if (health_facility_id == 2013 & village == 2026003)
replace dispensary = 1 if (health_facility_id == 2023 & village == 2025002)
replace dispensary = 1 if (health_facility_id == 2027 & village == 2019001)
replace dispensary = 1 if (health_facility_id == 2029 & village == 2030005)


***Results***

*Label variables
lab var mobile_clinic "Village served by mobile clinic"
lab var mobile_clinic_recent "Village served by mobile clinic setup within past 3 years"
lab var dispensary "Village has health dispensary"
	

*Produce Excel output

putexcel set "intermediate_outcomes.xlsx", sheet("Category 2 - Facility") modify

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes mobile_clinic mobile_clinic_recent dispensary
local row = 4
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}


*******************************************************************************

* CATEGORY 4. Bylaws, partnerships, or other interventions aimed at health system uptake

replace bylaw = 0 if bylaw == 2
tab bylaw

tab treatment, sum(bylaw)


***Results***

lab var bylaw "Village has by-law on MNH services"

*Produce Excel output

putexcel set "intermediate_outcomes.xlsx", sheet("Category 4") modify

local outcomes bylaw
local row = 5
foreach var of local outcomes {
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}

********************************************************************************

* CATEGORY 6. Improved attitude, effort, trust of the provider

	//Whether CHW assigned to village
replace chw_assigned = 0 if chw_assigned == 2
tab chw_assigned


	//Number of CHWs assigned to village
g chw_number_2 = chw_number
replace chw_number_2 = 0 if chw_assigned == 0

	//Whether CHWs live in village
replace chw_residence = 0 if chw_residence == 2
tab chw_residence

g chw_residence_2 = chw_residence
replace chw_residence_2 = 0 if chw_assigned == 0


***Results***

lab var chw_assigned "Village: Whether CHW assigned to sample village"
lab var chw_number_2 "Village: Number of CHWs assigned to sample village"
lab var chw_residence_2 "Village: Whether assigned CHWs live in village"

putexcel set "intermediate_outcomes.xlsx", sheet("Category 6_facility") modify

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes chw_assigned chw_number_2 chw_residence_2
local row = 8
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment $strata, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}


*******************************************************************************

clear


	
